package com.ht.log;

import com.ht.aems.util.ObjectBuffer;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Dong Zhukai
 * Date: 2009-10-25 16:10:53
 */
public class LoggerUtil {
    private static Logger log = Logger.getLogger(LoggerUtil.class);

    private static ObjectBuffer buffer;

    private static boolean isStoped = false;

    public static void initialBufferSize(int size) {
        buffer = new ObjectBuffer(size);
        Thread thread = new Thread() {
            public void run() {
                while (!isStoped) {
                    try {
                        save();
                        sleep(5000);
                    } catch (Exception e) {
                        log.warn("Exception when saving message to db: " + e);
                    }
                }
            }
        };
        thread.start();
    }

    private static void save() {
        Object[] msgs = buffer.read();
        if (msgs != null && msgs.length > 0) {
            MessagePersistLayer persist = MPLFactory.createMsgPersist();
            log.info("Saving message start... message length: " + msgs.length);
            for (Object obj : msgs) {
                Message m = (Message) obj;
                persist.addMessage(m);
            }
            log.info("Saving message end");
            persist.close();
        }
    }

    public static void saveLog(String userName, String moduleName, String deviceName, String content, int nLevel) {
        Message message = new Message();
        message.setDeviceRelated(deviceName);
        message.setModuleName(moduleName);
        message.setContent(content);
        message.setLevel(nLevel);
        if (nLevel > Message.MESSAGE_LEVEL_INFO)
            message.setNeedConfirm(true);
        else
            message.setNeedConfirm(false);
        message.setBeenConfirmed(false);
        message.setConfirmResult(false);
        message.setCreatedTime(new Date());
        message.setUserName(userName);
        buffer.write(new Object[]{message});
    }

    public static void saveLog(String userName, String moduleName, String deviceName, String content, int nLevel, long time) {
        Message message = new Message();
        message.setDeviceRelated(deviceName);
        message.setModuleName(moduleName);
        message.setContent(content);
        message.setLevel(nLevel);
        if (nLevel > Message.MESSAGE_LEVEL_INFO)
            message.setNeedConfirm(true);
        else
            message.setNeedConfirm(false);
        message.setBeenConfirmed(false);
        message.setConfirmResult(false);
        message.setCreatedTime(new Date(time));
        message.setUserName(userName);
        buffer.write(new Object[]{message});
    }

    public static void saveLog(String userName, String moduleName, String content, int nLevel) {
        Message message = createMessage(userName, moduleName, content, nLevel);
        buffer.write(new Object[]{message});
    }

    public static void saveLogImmediately(String userName, String moduleName, String content, int nLevel) {
        Message message = createMessage(userName, moduleName, content, nLevel);
        buffer.write(new Object[]{message});
        try {
            save();
        } catch (Exception e) {
            log.warn(e);
        }
    }

    private static Message createMessage(String userName, String moduleName, String content, int nLevel) {
        Message message = new Message();
        message.setUserName(userName);
        message.setModuleName(moduleName);
        message.setContent(content);
        message.setLevel(nLevel);
        if (nLevel > Message.MESSAGE_LEVEL_INFO)
            message.setNeedConfirm(true);
        else
            message.setNeedConfirm(false);
        message.setBeenConfirmed(false);
        message.setConfirmResult(false);
        message.setCreatedTime(new Date());
        message.setUserName(userName);
        return message;
    }

    public static void close() {
        isStoped = true;
    }
}
